package com.dindang.propertyfee.mvp.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.jess.arms.utils.ArmsUtils;

public class LetterListView extends View {

    OnTouchingLetterChangedListener onTouchingLetterChangedListener;
//    public static String[] b = {"A", "B", "C", "D", "E", "F", "G", "H","I", "J", "K",
//            "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    public static String[] b = {};
    public static int length = 26;
    int choose = -1;
    Paint paint = new Paint();
    boolean showBkg = false;
    private Context mContext;

    public LetterListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
    }

    public LetterListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    public LetterListView(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(b.length > 0){
            if (showBkg) {
                canvas.drawColor(Color.parseColor("#40000000"));
            }
            int height = getHeight();
            int width = getWidth();
            int singleHeight = height / length;
            for (int i = 0; i < b.length; i++) {
                paint.setColor(Color.parseColor("#2E2F30"));
                paint.setTextSize(ArmsUtils.dip2px(mContext,12));
                paint.setAntiAlias(true);
                float xPos = width / 2 - paint.measureText(b[i]) / 2;
//                float yPos = singleHeight * i + singleHeight;
                float yPos = ((height / 2) - singleHeight * (b.length / 2)) + singleHeight * i;
                canvas.drawText(b[i], xPos, yPos, paint);
                paint.reset();
            }
        }
    }

    public void onDraw(){

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int singleHeight = getHeight() / length;// 单行高度
        final float initialPosition = ((getHeight() / 2) - singleHeight * (b.length / 2));// 初始位置
        final float actualHeight = singleHeight * b.length;// 英文字母实际高度
        final int action = event.getAction();
        final float y = event.getY();
        final int oldChoose = choose;
        final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
        final int c = (int) ((y - initialPosition + singleHeight) / actualHeight * b.length);
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                showBkg = true;
                if (oldChoose != c && listener != null) {
                    if (c >= 0 && c < b.length) {
                        listener.onTouchingLetterChanged(b[c]);
                        choose = c;
                        invalidate();
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (oldChoose != c && listener != null) {
                    if (c >= 0 && c < b.length) {
                        listener.onTouchingLetterChanged(b[c]);
                        choose = c;
                        invalidate();
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                showBkg = false;
                choose = -1;
                invalidate();
                break;
        }
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    public void setOnTouchingLetterChangedListener(
            OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
        this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
    }

    public interface OnTouchingLetterChangedListener {
        void onTouchingLetterChanged(String s);
    }

}
